package com.biz.primus.ms.search.transform;

import com.biz.primus.base.elasticsearch.entities.ProductEntity;
import com.biz.primus.base.elasticsearch.entities.PropertyEntity;
import com.biz.primus.base.vo.ExtendProductVo;
import com.biz.primus.base.vo.search.ProductIdxVo;
import com.biz.primus.common.utils.ValueUtils;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;

/**
 * 转换器(ProductIdxVo --> ProductEntity)
 *
 * @author david-liu
 * @date 2017年01月22日
 * @reviewer
 */
public class ProductIdxVo2ProductEntity implements Function<ProductIdxVo, ProductEntity> {
    @Override
    public ProductEntity apply(ProductIdxVo vo) {
        ProductEntity entity = new ProductEntity();
        entity.setId(vo.getId());
        entity.setProductId(vo.getProductId());
        entity.setVendorId(String.valueOf(vo.getVendorId()));
        entity.setName(vo.getName());
        entity.setProductCode(vo.getProductCode());
        entity.setSalesVolume(ValueUtils.getValue(vo.getSalesVolume()));
        entity.setViewVolume(ValueUtils.getValue(vo.getViewVolume()));
        entity.setCategory(vo.getCategory());
        entity.setBrand(vo.getBrand());
        entity.setSaleTags(vo.getSaleTags());
        entity.setSaleTagIds(vo.getSaleTagIds());
        entity.setFinalPrice(vo.getPrice());
        entity.setOnSaleTime(vo.getOnSaleTime());
        entity.setDepotCode(vo.getDepotCode());
        entity.setStock(vo.getStock());
        entity.setGeoIds(vo.getGeoIds());
        entity.setSaleStatus(vo.getSaleStatus());
        entity.setAppSaleStatus(vo.getAppSaleStatus());
        entity.setWebSaleStatus(vo.getWebSaleStatus());
        entity.setProductType(vo.getProductType());
        entity.setGlobalIdx(vo.getGlobalIdx());
        entity.setDepotStock(vo.getDepotStock());
        entity.setSeoTitle(vo.getSeoTitle());
        entity.setSeoDescription(vo.getSeoDescription());
        entity.setSeoKeywords(vo.getSeoKeywords());
        List<ExtendProductVo> extendProductVoList = vo.getExtendProductVoList();
        if (CollectionUtils.isNotEmpty(extendProductVoList)){
            List<PropertyEntity> propertyEntities = Lists.newArrayList();
            for (ExtendProductVo extendProductVo : extendProductVoList){
                PropertyEntity propertyEntity = new PropertyEntity();
                propertyEntity.setName(extendProductVo.getKey());
                propertyEntity.setValue(extendProductVo.getValue());
                propertyEntities.add(propertyEntity);
            }
            entity.setPropertiesNested(propertyEntities);
        }

        return entity;
    }
}
